<html>
	<head>
		<title>CS143 Project 1B</title>
		<style type="text/css">
			body {
				font-family: Helvetica, Arial, sans-serif;
			}
			table, th, td {
				border-color: #c8c8c8;
				border-style: solid;
			}
			table {
				border-width: 0 0 1px 1px;
			}
			th, td {
				border-width: 1px 1px 0 0;
				padding: 4px;
			}
			.fields { background-color: #b8b8b8;}
			.odd-result { background-color: #d8d8d8; }
			.even-result { background-color: #fff;}
		</style>
	</head>
	<body>
		<form action="query.php" method="GET">
			<textarea name="query" rows="9" cols="72"><?php if (isset($_GET['query'])) echo $_GET['query']; ?></textarea><br />
			<input type="submit" value="Submit" />
		</form>
<?php
// Connect to the database
$db = mysql_connect('localhost', 'cs143', '');
if (!$db)
	die("Could not connect to MySQL!");
// Select CS143 database
mysql_select_db('CS143', $db);
$query = $_GET['query'];
if ($query){
		echo "<h2>Results From MySQL<strong></strong></h2>";
	if ($result = mysql_query($query)){
		echo "<table id=\"results\" cellpadding=\"1\" cellspacing=\"0\">";
		echo "<thead class=\"fields\">";
		// get the number of fields
		$num_fields = mysql_num_fields($result);
		echo "<tr>";
		// iterate through all the the field names
		// and print them
		for ($i = 0; $i < $num_fields; $i++){
			$field = mysql_fetch_field($result, $i);
			echo "<th>{$field->name}</th>";
		}
		echo "</tr>";
		echo "</thead>";
		echo "<tbody>";
		// fetch each row from the results...
		$count = 0;
		while ($row = mysql_fetch_array($result)){
			echo "<tr class=\"". ($count ? "odd-result" : "even-result") . "\">";
			// ...and print each field
			for ($i = 0; $i < $num_fields; $i++){
				echo "<td>" . ($row[$i] ? $row[$i]: "N/A") . "</td>";
			}
			echo "</tr>";
			$count = ++$count % 2;
		}
		echo "</tbody>";
		echo "</table>";
	} else {
		// An error occurred, so print it.
		echo mysql_error();
	}
}
mysql_close($db);
?>
	</body>
</html>